package assignment1;

import java.io.*;
import java.net.Socket;
import java.util.Random;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class GradeClient extends Thread {
    public static final String LOCAL_HOST = "127.0.0.1";
    public static final int PORT = 8888;


    public GradeClient(String name) {
        super(name);
    }

    @Override
    public void run() {
        try (   Socket socket = new Socket(LOCAL_HOST, PORT);
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();) {
            //向服务器端发送一条消息, 发完就关掉去
            os.write(String.valueOf(getGrade()).getBytes());
            os.flush();
            socket.shutdownOutput();

            //读取服务器返回的消息
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            // assignment1.GradeClient.run(GradeClient.java:36)
            String mess = br.readLine();
            System.out.println(this.getName() + " ：" + mess);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 将客户端录入的十个数传入服务器端,
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 10; i++) {
            new GradeClient("线程 " + i).start();
        }
    }

    static int getGrade() {
        return new Random().nextInt(100) + 1;
    }

}
